<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>形状几何体-ShapeGeometry</title>
    <style>
      body {
        margin: 0;
        overflow: hidden;
        /* 隐藏body窗口区域滚动条 */
      }
    </style>
    <!--引入three.js三维引擎-->
    <!-- <script src="http://www.yanhuangxueyuan.com/versions/threejsR92/build/three.js"></script> -->
    <!-- <script src="http://www.yanhuangxueyuan.com/threejs/build/three.js"></script> -->
    <!-- Three.js-master 包示例中的用法 examples/js/controls/OrbitControls.js  版本要匹配-->
    <script src="http://www.yanhuangxueyuan.com/3D/example/three.min.js"></script>
    <script src="../../util/OrbitControls.js"></script>
  </head>
  <body>
    <script type="module">
      import {
        RAFTime,
        sphereMesh,
        cylinderMesh,
        v2ToV3,
      } from "../../util/index.js";
      /**
       * 创建场景对象Scene
       */
      var scene = new THREE.Scene();

      /**
       * 辅助坐标系 参数250表示坐标系大小，可以根据场景大小去设置
       */
      var axisHelper = new THREE.AxisHelper(1000);
      scene.add(axisHelper);

      //------------------------------------------- Shape，直接用点
      // var points = [
      //   new THREE.Vector2(-50, -50),
      //   new THREE.Vector2(-60, 0),
      //   new THREE.Vector2(0, 50),
      //   new THREE.Vector2(60, 0),
      //   new THREE.Vector2(50, -50),
      //   new THREE.Vector2(-50, -50),
      // ];
      // // 通过顶点定义轮廓
      // var shape = new THREE.Shape(points);
      // // shape可以理解为一个需要填充轮廓
      // // 所谓填充：ShapeGeometry算法利用顶点计算出三角面face3数据填充轮廓
      // var geometry = new THREE.ShapeGeometry(shape, 25);

      //------------------------------------------- Shape，使用Shape圆弧作为外轮廓，使用path作为内轮廓
      // var shape = new THREE.Shape(); //Shape对象
      // //外轮廓
      // shape.arc(0, 0, 100, 0, 2 * Math.PI);
      // // 内轮廓1
      // var path1 = new THREE.Path();
      // path1.arc(0, 0, 40, 0, 2 * Math.PI);
      // // 内轮廓2
      // var path2 = new THREE.Path();
      // path2.arc(80, 0, 10, 0, 2 * Math.PI);
      // // 内轮廓3
      // var path3 = new THREE.Path();
      // path3.arc(-80, 0, 10, 0, 2 * Math.PI);
      // //三个内轮廓分别插入到holes属性中
      // shape.holes.push(path1, path2, path3);
      // var geometry = new THREE.ShapeGeometry(shape, 100);

      // ------------------------------------------- Shape，使用Shape走线作为外轮廓，使用path作为内轮廓
      var shape = new THREE.Shape(); //Shape对象
      //外轮廓
      shape.moveTo(0, 0); //起点
      shape.lineTo(0, 100); //第2点
      shape.lineTo(100, 100); //第3点
      shape.lineTo(100, 0); //第4点
      shape.lineTo(0, 0); //第5点

      //内轮廓
      var path = new THREE.Path(); //path对象
      path.arc(50, 50, 40, 0, 2 * Math.PI); //圆弧
      // 不知为何 path的走线 作为内轮廓不行 可能是版本问题85
      // path.moveTo(20, 20); //起点
      // path.lineTo(20, 80); //第2点
      // path.lineTo(80, 80); //第3点
      // path.lineTo(80, 20); //第4点
      // path.lineTo(20, 20); //第5点

      shape.holes.push(path); //设置内轮廓
      var geometry = new THREE.ShapeGeometry(shape, 20);

      var material = new THREE.MeshPhongMaterial({
        color: 0x0000ff, //三角面颜色
        side: THREE.DoubleSide, //两面可见
        // wireframe: true, //线框
      }); //材质对象

      var mesh = new THREE.Mesh(geometry, material); //旋转网格模型对象
      scene.add(mesh); //旋转网格模型添加到场景中

      // 平行光
      var directionalLight = new THREE.DirectionalLight(0xffffff, 1);
      // 平行光辅助对象
      const directionalLightHelper = new THREE.DirectionalLightHelper(
        directionalLight,
        10
      );
      scene.add(directionalLightHelper);
      // 设置光源位置
      directionalLight.position.set(45, 75, 30);
      scene.add(directionalLight);
      // 设置用于计算阴影的光源对象
      directionalLight.castShadow = true;
      // 设置计算阴影的区域，最好刚好紧密包围在对象周围
      // 计算阴影的区域过大：模糊  过小：看不到或显示不完整
      directionalLight.shadow.camera.near = 0.5;
      directionalLight.shadow.camera.far = 300;
      directionalLight.shadow.camera.left = -100;
      directionalLight.shadow.camera.right = 100;
      directionalLight.shadow.camera.top = 200;
      directionalLight.shadow.camera.bottom = -200;
      // 设置mapSize属性可以使阴影更清晰，不那么模糊
      directionalLight.shadow.mapSize.set(1024, 1024);
      console.log(directionalLight.shadow.camera);

      //环境光
      var ambient = new THREE.AmbientLight(0x000000);
      scene.add(ambient);

      /**
       * 相机设置
       */
      var width = window.innerWidth; //窗口宽度
      var height = window.innerHeight; //窗口高度
      var k = width / height; //窗口宽高比
      var s = 100; //三维场景显示范围控制系数，系数越大，显示的范围越大
      //创建相机对象
      var camera = new THREE.OrthographicCamera(-s * k, s * k, s, -s, 1, 1000);
      camera.position.set(60, 60, 60); //设置相机位置
      camera.lookAt(scene.position); //设置相机方向(指向的场景对象)

      /**
       * 创建渲染器对象
       */
      var renderer = new THREE.WebGLRenderer();
      renderer.setSize(width, height); //设置渲染区域尺寸
      renderer.setClearColor(0xb9d3ff, 1); //设置背景颜色
      // 设置渲染器，允许光源阴影渲染
      renderer.shadowMap.enabled = true;
      document.body.appendChild(renderer.domElement); //body元素中插入canvas对象

      // // 监听鼠标、键盘事件，实现 旋转/缩放/平移
      // function render() {
      //   renderer.render(scene, camera); //执行渲染操作
      // }

      // 递归函数,每1/60秒左右,调用一次自己
      const render = RAFTime((t) => {
        renderer.render(scene, camera); //执行渲染操作
        // line.rotateY(0.001 * t);
      });

      // 启动渲染
      render();

      // 创建，轨迹控件
      var controls = new THREE.OrbitControls(camera, renderer.domElement);
      // 监听鼠标、键盘事件
      controls.addEventListener("change", render);
    </script>
  </body>
</html>
